var setting = {
    showList: true,
    page: {
        pageNo: 10,
        total: 20,
        pageSize: 10,
        dataList: []
    },
    queryParam: {
        name: '',
    },
    text: {
        submitButtonText:'添加',
    },
    options: [],
    selectedData: [],
    form: {
        role_id:'',
        name: '',
        memo:''
    },
    rules: {
        name: [
            { required: true, message: '请输入名称', trigger: 'blur' },
            { min: 1, max: 15, message: '长度在 1 到 15 个字符', trigger: 'blur' },
        ]
    },
    loading: false,
    dialogVisible: false,
    treeData: [],
    defaultProps: {
        children: 'children',
        label: 'title',
        id: 'menu_id'
    },
    role_id: '',
    isAdd: true,
}
var vue = new Vue({
    el: '.main',
    data: setting,
    methods: {
        handleCurrentChange: function (pageNo) {//分页查询
            page(pageNo)
        },
        handleSizeChange: function (pageSize) {//修改pageSize查询
            vue.page.pageSize=pageSize
            page(1)
        },
        query: function () {//条件查询
            page(1)
        },
        getSelected: function (rows) {//获取选中内容
            vue.selectedData = rows;
        },
        changeAddOrEdit: function (type) {//切换添加
            if(type == 'add'){
                vue.text.submitButtonText = '添加';
                vue.isAdd = true;
            }else {
                vue.text.submitButtonText = '修改';
                var selectedData = vue.selectedData;
                if(selectedData.length == 0){
                    vue.$message({
                        message: '请选择要修改的数据',
                        type: 'warning'
                    })
                    return ;
                }else if(selectedData.length > 1){
                    vue.$message({
                        message: '请选择一条要修改的数据',
                        type: 'warning'
                    })
                    return ;
                }else {
                    vue.isAdd = false;
                    $.post('/role/getRoleById',{role_id: selectedData[0].role_id}, function (role) {
                        vue.form = role;
                    })
                }
            }
            vue.showList = false;
        },resetForm: function () {
            this.$refs.form.resetFields();
        },
        goBack: function () {
            vue.showList = true;
            vue.resetForm();
        },
        addOrEdit: function () {
            vue.loading = true;
            vue.$refs['form'].validate(function (v) {
                if(v){
                    var data = vue.form;
                    var url = (vue.isAdd?'/role/add':'/role/edit');
                    data.role_id = data.role_id == ''?0:data.role_id;
                    $.post(url, data, function (msg) {
                        vue.loading = false;
                        if(msg.status == 'SUCCESS'){
                            vue.$message({
                                message: msg.message,
                                type: 'success'
                            })
                            vue.showList = true;
                            page(vue.page.pageNo)
                            vue.resetForm();
                        }else{
                            vue.$message.error(msg.message);
                        }
                    })
                }
            })
        },
        del: function () {
            var data = vue.selectedData;
            if(data.length == 0){
                vue.$message({
                    message: '请选择要删除的数据',
                    type: 'warning'
                })
                return ;
            }
            this.$confirm('此操作将永久删除选中数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function () {
                $.ajax({
                    url : "/role/delete",
                    data : JSON.stringify(data),
                    type : "POST",
                    dataType : "json",
                    contentType :  "application/json;charset=utf-8",
                    success: function (msg) {
                        vue.$message({
                            message: msg.message,
                            type: 'success'
                        })
                        page(vue.page.pageNo)
                    }
                })
            }).catch(function () {
                vue.$message({
                    message: '已取消',
                    type: 'success'
                })
            })
        },
        getRoleMenu: function (row) {
            vue.dialogVisible = true;
            vue.role_id = row.role_id;
            $.post('/role/getRoleMenu',{role_id: row.role_id},function (data) {
                vue.treeData = data.allMenu;
                var checked = [];
                data.roleMenu.forEach(function (menu) {
                    checked.push(menu.menu_id)
                })
                vue.$refs['tree'].setCheckedKeys(checked);
            })
        },
        confirm: function () {
            vue.dialogVisible = false;
            var data = {
                role_id: vue.role_id
            }
            vue.$refs['tree'].getCheckedNodes().forEach(function (menu, index) {
                data['menus['+index+'].menu_id'] = menu.menu_id;
            })
            $.post('/role/allotPower',data, function (msg) {
                if(msg.status == 'SUCCESS'){
                    vue.$message.success(msg.message)
                }else{
                    vue.$message.warning(msg.message)
                }
            })
        }
    }
})
page(1)
function page(page) {
    var data = {
        page: page,
        pageSize: vue.page.pageSize,
        name: vue.queryParam.name,
    }
    $.post('/role/queryByPage', data, function (page) {
        vue.page.dataList = page.rows;
        vue.page.pageNo = page.page;
        vue.page.total = page.total;
    })
}